home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.07 Jul 96 / 01 - popup Applet / popup.java < prev    next >
Encoding:
Java Source  |  1996-04-05  |  1.4 KB  |  57 lines  |  [TEXT/CWIE]

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.net.*;
  4.  
  5. public class popup extends Applet
  6. {
  7.     private Choice    urlChoices;
  8.     int                numURLs;
  9.     
  10.     public void    init()
  11.     {
  12.         this.setBackground( Color.yellow );//java.awt.Color
  13.         this.setForeground( Color.red );//java.awt.Color
  14.         
  15.         urlChoices = new Choice();
  16.         
  17.         urlChoices.addItem( "www.metrowerks.com" );
  18.         urlChoices.addItem( "www.netscape.com" );
  19.         urlChoices.addItem( "www.mactech.com" );
  20.         
  21.         urlChoices.setForeground( Color.green );
  22.         urlChoices.setBackground( Color.blue );
  23.         
  24.         this.add( new Label( "Select URL: " ) );
  25.         this.add( urlChoices );
  26.     }
  27.     
  28.     public boolean action( Event e, Object obj )
  29.     {
  30.         URL        url;
  31.         
  32.         if ( e.target == urlChoices )
  33.         {
  34.             try
  35.             {
  36.                 url = new URL( obj.toString() );
  37.             }
  38.             catch( MalformedURLException err )
  39.             {
  40.                 // Could print error message here.
  41.                 return true;
  42.             }
  43.             /* If a method throws an exception, you must be prepared to catch it,
  44.                 even if you ignore the result. See java.net.URL().
  45.                 If I throw an exception, I'll create a new java.lang.exception
  46.                 object (or some class that extends throwable). But if I catch an
  47.                 exception, the exception object will be passed to me, just like err was here.
  48.                 Take a look at Throwable to see what you can do with err (especially getMessage()).
  49.                 
  50.             */
  51.             this.getAppletContext().showDocument( url );
  52.             return true;
  53.         }
  54.         else
  55.             return super.action( e, obj );
  56.     }
  57. }